package com.hcmiu.buddytracking;

import org.json.JSONException;
import org.json.JSONObject;

import setMethods.setPassword;
import Parser.StringtoMD5;
import Utils.toastMaking;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class ChangePassword extends Activity {
	private Button btnChangePassword;
	private Button btnCancel;
	private EditText oldPassword;
	private EditText newPassword;
	private EditText confirmPassword;
	private SharedPreferences pref;
	private String currentPhoneNumber;
	private String PREF_NAME = "buddytracking";
	private ProgressDialog progressDialog;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// progress
		progressDialog = new ProgressDialog(this);
		progressDialog.setMessage("Please wait...");
		progressDialog.setIndeterminate(true);
		progressDialog.setCancelable(false);
		// //
		pref = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
		currentPhoneNumber = pref.getString("currentPhoneNumber", null);
		setContentView(R.layout.changepassword);

		btnChangePassword = (Button) findViewById(R.id.changepasswordbutton);
		btnCancel = (Button) findViewById(R.id.cancelPasswordbutton);
		oldPassword = (EditText) findViewById(R.id.oldpassword);
		newPassword = (EditText) findViewById(R.id.newpassword);
		confirmPassword = (EditText) findViewById(R.id.confirmpassword);

		btnChangePassword.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				String oldpassword = oldPassword.getText().toString();
				String newpassword = newPassword.getText().toString();
				String confirmpassword = confirmPassword.getText().toString();
				if (oldpassword.length() == 0 && newpassword.length() == 0
						&& confirmpassword.length() == 0) {					
					toastMaking.errorToast(getApplicationContext(),
							"Fill in all information !").show();
				} else if (newpassword.length() <= 3
						&& confirmpassword.length() <= 3) {					
					toastMaking.errorToast(getApplicationContext(),
							"Minimum password is 4 characters").show();
				} else if (newpassword.contains("'")) {					
					toastMaking.errorToast(getApplicationContext(),
							"Remove ' character from your password").show();
				} else if (!newpassword.equals(confirmpassword)) {					
					toastMaking.errorToast(getApplicationContext(),
							"New paswords do not match").show();
				} 
				else if (oldpassword.equals(newpassword)) {				
					toastMaking.errorToast(getApplicationContext(),
					"Old & New passwords must be different").show();
				}
				else if (newpassword.equals(confirmpassword)) {
					progressDialog.show();
					doChangePassword(oldpassword, newpassword);
				}

			}
		});
		btnCancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				ChangePassword.this.finish();
			}
		});
	}

	private void doChangePassword(final String oldpassword,
			final String newpassword) {
		Thread t = new Thread() {
			@Override
			public void run() {
				Looper.prepare();
				boolean result = false;
				try {
					result = checkOldPassword(oldpassword);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (result == true) {
					try {
						changePassword(newpassword);
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					progressDialog.dismiss();
					toastMaking.infoToast(getApplicationContext(),
							"Password changed successfully").show();

					ChangePassword.this.finish();
				} else if (result == false) {
					progressDialog.dismiss();
					toastMaking.errorToast(getApplicationContext(),
							"Old Password is not correct").show();
				}
				Looper.loop();
			}
		};
		t.start();
	}

	private void changePassword(String newpassword) throws JSONException {
		String newpassword_md5 = StringtoMD5.MD5(newpassword);
		JSONObject object = new JSONObject();
		object.put("phoneNumber", currentPhoneNumber);
		object.put("newpaswword", newpassword_md5);
		setPassword.process(object.toString());
	}

	private boolean checkOldPassword(String oldpassword) throws JSONException {
		boolean result = false;
		String oldpassword_md5 = StringtoMD5.MD5(oldpassword);
		JSONObject object = new JSONObject();
		object.put("phoneNumber", currentPhoneNumber);
		object.put("oldpaswword", oldpassword_md5);
		if ((getFromDBMethods.checkOldPassword.process(object.toString()))
				.length() == 32) {
			result = true;
		}
		return result;
	}

}
